﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MINERP.MODEL.FoundationModel;
using MINERP.DAL;

namespace MINERP.BLL.Statistics
{
    public class NewInnerReturnBLL
    {
        public List<NewInnerReturn_Info> GetAllInnerReturn(DateTime dtStart, DateTime dtEnd, List<int> selectedUser)
        {
            List<NewInnerReturn_Info> result = new List<NewInnerReturn_Info>();
            //List<NewInnerReturn> tempR = new List<NewInnerReturn>();
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                //tempR = db.NewInnerReturn.Where(i => i.CreateTime >= dtStart && i.CreateTime <= dtEnd && selectedUser.Contains(i.ReturnUserId)).ToList();
                //db.NewInnerReturn_BK.Where(i => i.CreateTime >= dtStart && i.CreateTime<= dtEnd && selectedUser.Contains(i.ReturnUserId)).ToList()
                result.AddRange(ConvertToInfo(db.NewInnerReturn.Where(i => i.CreateTime >= dtStart && i.CreateTime <= dtEnd ).ToList().Where(i=>selectedUser.Contains(i.ReturnUserId)).ToList(), db));
                result.AddRange(ConvertToInfo(db.NewInnerReturn_BK.Where(i => i.CreateTime >= dtStart && i.CreateTime <= dtEnd).ToList().Where(i => selectedUser.Contains(i.ReturnUserId)).ToList(), db));
            }

            return result;
        }

        public List<NewInnerReturn_Info> ConvertToInfo(List<NewInnerReturn_BK> source, newSampleYPDBEntities db)
        {
            List<NewInnerReturn_Info> result = new List<NewInnerReturn_Info>();
            foreach (var ni in source)
            {
                result.Add(new NewInnerReturn_Info()
                {
                    CreateTime = ni.CreateTime,
                    CreateUserID = ni.CreateUserId,
                    CreateUserName = db.users.FirstOrDefault(u => u.userID == ni.CreateUserId).UserName,
                    Height = (float)ni.Heigth,
                    Width = (float)ni.Width,
                    InnerReturnID = ni.InnerReturnID,
                    OrderProductID = ni.OrderproductID,
                    PhotoArea = (float)ni.PhotoAreas,
                    PhotoID = ni.PhotoId,
                    PicPath = ni.PicPath,
                    Remark = ni.Remark,
                    ReturnTechFromName = db.tb_Tech.FirstOrDefault(t => t.TechId == ni.ReturnTechFrom).TechName,
                    ReturnTechToName = db.tb_Tech.FirstOrDefault(t => t.TechId == ni.ReturnTechTo).TechName,
                    ReturnUserName = db.users.FirstOrDefault(u => u.userID == ni.ReturnUserId).UserName,
                    ReturnUserID = ni.ReturnUserId
                });
            }

            return result;
        }

        public List<NewInnerReturn_Info> ConvertToInfo(List<NewInnerReturn> source, newSampleYPDBEntities db)
        {
            List<NewInnerReturn_Info> result = new List<NewInnerReturn_Info>();
            foreach (var ni in source)
            {
                result.Add(new NewInnerReturn_Info()
                {
                    CreateTime = ni.CreateTime,
                    CreateUserID = ni.CreateUserId,
                    CreateUserName = db.users.FirstOrDefault(u => u.userID == ni.CreateUserId).UserName,
                    Height = (float)ni.Heigth,
                    Width = (float)ni.Width,
                    InnerReturnID = ni.InnerReturnID,
                    OrderProductID = ni.OrderproductID,
                    PhotoArea = (float)ni.PhotoAreas,
                    PhotoID = ni.PhotoId,
                    PicPath = ni.PicPath,
                    Remark = ni.Remark,
                    ReturnTechFromName = db.tb_Tech.FirstOrDefault(t => t.TechId == ni.ReturnTechFrom).TechName,
                    ReturnTechToName = db.tb_Tech.FirstOrDefault(t => t.TechId == ni.ReturnTechTo).TechName,
                    ReturnUserName = db.users.FirstOrDefault(u => u.userID == ni.ReturnUserId).UserName,
                    ReturnUserID = ni.ReturnUserId
                });
            }

            return result;
        }
    }
}
